package controller;

import entity.Menu;
import entity.EntityList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import service.MenuService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("MenuController")
public class MenuController extends Uploads{
     @Autowired
     MenuService ms;

     //批量修改材料
    @ResponseBody
    @RequestMapping("updateMs")
     public Integer updateMs(EntityList entityList){
   //     System.out.print("asdsa");
         return ms.updateMs(entityList.getMsList());
     }

    //批量修改步骤
    @ResponseBody
    @RequestMapping("updateStep")
    public Integer updateStep(HttpServletRequest req,EntityList entityList) throws IOException {
        //步骤图片
        for (int i = 0; i < entityList.getStepList().size(); i++) {
           System.out.print(entityList.getStepList().get(i).getSimg());
            System.out.print(entityList.getStepList().get(i).getSdesc());
            System.out.print(entityList.getStepList().get(i).getStitle());
            System.out.print(entityList.getStepList().get(i).getSmenuid());
        }
        return ms.updateStep(entityList.getStepList());
    }

    //修改菜谱详情页的菜谱信息
    @ResponseBody
    @RequestMapping("updateMenu")
    public Integer updateMenu(HttpServletRequest req,Menu m,MultipartFile menuFile) throws IOException {
        //将原来的图片在服务器里删了
 //       System.out.print(m.getMtitle()+"asda");

        String imgName=m.getMimg().substring(m.getMimg().lastIndexOf("/"));
        File f=new File(req.getServletContext().getRealPath("/images/menu")+"/"+imgName);
        f.delete();//Boolean flag=f.delete();
        //修改新的图片和菜谱信息
        String menuStr=uploads(req,menuFile,"images/menu");
        m.setMimg(menuStr);
        return ms.updateMenu(m);
    }

    //菜谱详情页 根据菜谱id查询菜谱对应材料和步骤信息(返回Map)
    @ResponseBody
    @RequestMapping("findMenuAll")
    public Map findMenuAll(Integer menuid){
        return ms.findMenuAll(menuid);
    }

     //删除服务器中的图片
      @ResponseBody
      @RequestMapping("deleteImg")
      public Boolean deleteImg(HttpServletRequest req){
          File f=new File(req.getServletContext().getRealPath("/images/menu")+"/1587031806366.jpg");
          Boolean flag=f.delete();
          return flag;
      }


    //添加菜谱表和步骤表以及材料表信息(包括单张和多张图片上传)
    @ResponseBody
    @RequestMapping("addMenuAll")
    public Integer addMenuAll(HttpServletRequest req,EntityList entityList, Menu menu, MultipartFile[] stepFile, MultipartFile menuFile) throws IOException {
        //EntityList :实体类的映射
        //MultipartFile[] stepFile:二进制文件数组
          //步骤图片
            for (int i = 0; i < stepFile.length; i++) {
                MultipartFile filex = stepFile[i];
                //将多个步骤图片存入服务器的images/step文件下
                String newString=uploads(req,filex,"images/step");
                entityList.getStepList().get(i).setSimg(newString);
            }
            //菜谱图片
            //将一个菜谱图片存入服务器的images/menu文件夹下
               String menuStr=uploads(req,menuFile,"images/menu");
               menu.setMimg(menuStr);
             //添加三个表的数据
              int result= ms.addMenuAll(menu,entityList);
            return result;
    }


    //查询菜谱列表(根据用户角色id)
    @ResponseBody
    @RequestMapping("findAll")
    public Map findAll(Integer delstatus, Integer uroleid, Integer page, Integer count, HttpSession session){
        System.out.print(delstatus+"s"+uroleid+"q"+page+"a"+count+"asda");
        //传入删除状态 1查询菜谱列表 2查询被物理删除的菜谱列表
        return  ms.findAll(delstatus,uroleid,page,count,session);
    }

    //根据菜谱id物理删除或者恢复一个菜谱,修改删除状态
    @ResponseBody
    @RequestMapping("deleteById")
    public int deleteById(Integer menuid,Integer number){
        //number：2是物理删除操作传入要物理删除的menuid
        //1是恢复操作传入要恢复的menuid
        //3是永久删除
        int i=ms.updateDelstatus(menuid,number);
        return i;
    }

    //查询菜谱类型
    @ResponseBody
    @RequestMapping("findType")
    public List findType(){
        return ms.findType();
    }

}
